<?php include_once("../../../common/common_setting_unit.php");         // 引入通用的设置文件 ?>
<?php include_once(ROOT_PATH_ . "/controller/common/module/head.php"); // 引入通用的头部 ?>


<? $system_part = $_REQUEST ['sys_part']; ?>



<?php
$new_term_nid = addslashes($_REQUEST['term_nid']);

//传递过来的页面信息。返回的时候，会根据这些信息返回原来的页面
$nowpageid = $_REQUEST['nowpageid'];
$sortby    = $_REQUEST['sortby'];

?>







<?
//设置需要搜索的表格的前缀后缀
$table_Suffix = "term";
$table = $table_Prefix . $table_Suffix;
//设置需要搜索的表格的前缀后缀
?>



<?php

$brand_list_json = json_decode($_REQUEST['brand_list_json'],true);  //品牌关联

//var_dump($brand_list_json);
//exit;

$new_brand_list = '';

if($_REQUEST['brand_list_json'] != '[]'){
	$brand_list_arr = array();
	foreach($brand_list_json as $brand_val){
		
		if(!empty($brand_val['children'])){
			$brand_list_arr [] = get_deep_id($brand_val['children']);
		}
		else{
			$brand_list_arr [] = $brand_val['id'];
		}
		
	}
	static $res_arr = array();
	$new_brand_list = toOneArray($brand_list_arr,true);
}

function toOneArray($array,$clear=false)
{
    static $res_arr = array();
	if ($clear)
    {
        $res_arr = array();
    }
    foreach ($array as $v) {
        if (is_array($v)) {
            toOneArray($v);
        } else {
            $res_arr[] = $v;
        }
    }
    return $res_arr;
}

function get_deep_id($list){
	$id_arr = array();
	foreach($list as $val){
		if(!empty($val['children'])){
			return  get_deep_id($val['children']);
		}
		
		$id_arr[] = $val['id'];
		
	}
	
	return $id_arr;
}




    //这里是设计上传文件的部分：包含图片和附件。视频也应该用这种形式上传
    //统一设置一下保存图片的路径
    $_uf['path']        = ROOT_PATH_ . "/uploadfiles/".$theme_folder."/";





    //上传icon
    if (is_uploaded_file($_FILES['icon']['tmp_name']))
    {
        $_uf['upfile']       = $_FILES['icon'];
        $_info               = upload_file($_uf);
        $_REQUEST['ai_icon'] = $_info['name'];
        $icon          = $_REQUEST['ai_icon'];


    }

    //上传Banner
    if (is_uploaded_file($_FILES['banner']['tmp_name']))
    {
        $_uf['upfile']       = $_FILES['banner'];
        $_info               = upload_file($_uf);
        $_REQUEST['ai_banner'] = $_info['name'];
        $banner        = $_REQUEST['ai_banner'];

    }

    //
    if (is_uploaded_file($_FILES['extend_img']['tmp_name']))
    {
        $_uf['upfile']       = $_FILES['extend_img'];
        $_info               = upload_file($_uf);
        $_REQUEST['ai_extend_img'] = $_info['name'];
        $extend_img        = $_REQUEST['ai_extend_img'];

    }


?>


<?php
$new_term_mid         =addslashes($_REQUEST['term_mid']);
$new_term_name        =addslashes($_REQUEST['term_name']);
$new_term_en_name        =addslashes($_REQUEST['term_en_name']);
$new_term_title       =addslashes($_REQUEST['term_title']);
$new_term_keyword     =addslashes($_REQUEST['term_keyword']);
$new_term_description =addslashes($_REQUEST['term_description']);
$new_term_url         =addslashes($_REQUEST['term_url']);
$new_term_introduce   =addslashes($_REQUEST['term_introduce']);
$new_term_name        =addslashes($_REQUEST['term_name']);
$new_term_detail      =addslashes($_REQUEST['term_detail']);

$new_term_listorder   =addslashes($_REQUEST['term_listorder']);
$new_term_hotsale     =addslashes($_REQUEST['term_hotsale']);

$new_term_recommend   =addslashes($_REQUEST['term_recommend']);

$new_term_publish     =addslashes($_REQUEST['term_publish']);

$new_term_clickrate   =addslashes($_REQUEST['term_clickrate']);
$new_term_modifytime  =addslashes($_REQUEST['term_modifytime']);

$new_term_id          =addslashes($_REQUEST['term_term_id']);


$new_term_link        =addslashes($_REQUEST['term_link']);
$new_term_mark        =addslashes($_REQUEST['term_mark']);
$new_term_markorder   =addslashes($_REQUEST['term_markorder']);
$new_term_vip         =addslashes($_REQUEST['term_vip']);


$new_term_term_id     =addslashes($_REQUEST['term_term_id']);
$new_term_is_disabled =addslashes($_REQUEST['term_is_disabled']);


$new_term_banner_title =addslashes($_REQUEST['term_banner_title']);
$new_term_banner_url =addslashes($_REQUEST['term_banner_url']);



//根据interm来判断修改所属上级分类，应该改哪个项
if($new_term_interm=='term1')
{
	$sql_str=" term1 = '$new_term_term_id' ,";
}
else
{
	$sql_str=" term2 = '$new_term_term_id' ,";
}

?>






<?php
/*因为不能对分类进行升降级所以省略掉更新关于分类层
//此段函数，根据获取过来的id，查询出所属分类的term1~term5，interm
if($new_term_id == 'root')
{
	$trans_term_term0=0;
    $trans_term_interm='root';

}

else
  { 
  
       
 $result = mysql_query("select * from $table where nid = $new_term_id");
//根据ID,获取相应的所属分类资料
while($row = mysql_fetch_array($result))
  {
  
  $trans_term_nid=$new_term_id;
  $trans_term_term0=$row['term0'];
  $trans_term_term1=$row['term1'];
  $trans_term_term2=$row['term2'];
  $trans_term_term3=$row['term3'];
  $trans_term_term4=$row['term4'];
  $trans_term_term5=$row['term5'];
  $trans_term_interm=$row['interm'];
}
//下面这个判断，选择root没问题，其他的if else要修改一下
 if ($trans_term_interm == "root")
 {$trans_term_term1=$new_term_id;$trans_term_interm="term1";}
 
 else if ($trans_term_interm == "term1")
 {$trans_term_term2=$trans_term_nid;$trans_term_interm="term2";}
 
 else if ($trans_term_interm == "term2")
 {$trans_term_term3=$trans_term_nid;$trans_term_interm="term3";}
 
 else if ($trans_term_interm == "term3")
 
 {$trans_term_term4=$trans_term_nid;$trans_term_interm="term4";}
 
 else if ($trans_term_interm == "term4")
 {$trans_term_term5=$trans_term_nid;$trans_term_interm="term5";}
 
 

}
 
mysql_close($con);
*/


 
?>







<?

 $sql = "update $table SET 
mid              = '$new_term_mid',
title            = '$new_term_title',
name             = '$new_term_name',
en_name             = '$new_term_en_name',
keyword          = '$new_term_keyword',
description      = '$new_term_description',
url              = '$new_term_url',

introduce        = '$new_term_introduce',
detail           = '$new_term_detail',
hotsale          = '$new_term_hotsale',
recommend        = '$new_term_recommend',
listorder        = '$new_term_listorder' ,
clickrate        = '$new_term_clickrate' ,
modifytime       = '$new_term_modifytime',
publish          = '$new_term_publish',".$sql_str."
icon          = '$icon',
term_banner          = '$banner',

link             = '$new_term_link',
mark             = '$new_term_mark',
markorder        = '$new_term_markorder',
vip_term         = '$new_term_vip',

banner_title         = '$new_term_banner_title',
banner_url         = '$new_term_banner_url',

shop_brand         = '".implode(',',$new_brand_list)."',

is_disabled      = '$new_term_is_disabled'
where nid        = $new_term_nid";

//因为不能对分类进行升降级所以省略掉更新关于分类层级的语句,term0='$trans_term_term0',term1='$trans_term_term1',term2='$trans_term_term2',term3='$trans_term_term3',term4='$trans_term_term4',term5='$trans_term_term5',interm='$trans_term_interm'

sql_log($sql);
$query = mysql_query($sql) or die ('此次数据库操作失败');









if($new_brand_list){
	//先查品牌下已经存在什么商品
	$sql = "select * from ".$table_Prefix."relation where term_nid in(".implode(',',$new_brand_list).") and belong = 'brand' group by detail_nid ";
	
	$res = mysql_query($sql);
 	
	$brand_goods = array();
	while($row = mysql_fetch_assoc($res))
    {
       $brand_goods[$row['nid']]=$row['detail_nid'];
    }
	
	//查出门店有什么商品
	$sql = "select * from ".$table_Prefix."relation where term_nid = '$new_term_nid' and belong = 'shop'  group by detail_nid ";
	
	$res = mysql_query($sql);
 	
	$shop_goods = array();
	while($row = mysql_fetch_assoc($res))
    {
       $shop_goods[$row['nid']]=$row['detail_nid'];
    }
	
	$same_goods = array();
	$diff_goods = array();
	
	
	foreach($shop_goods as $skey=>$sval){
		if(in_array($sval,$brand_goods)){
			
			$same_goods_key[] = $skey;
			
			foreach($brand_goods as $bkey=>$bval){
				if($bval == $sval){
					unset($brand_goods[$bkey]); //删除相同的
				}
			}
		}
		else{
			$diff_goods[] = $sval;
		}
	}
	
	
	if(!empty($diff_goods)){
		
		$sql = "delete FROM  ".$table_Prefix."relation where belong = 'shop' and term_nid = '$new_term_nid' and detail_nid in(".implode(',',$diff_goods).") "; //删除本店不符合的商品
		
		mysql_query($sql);
	}
	

	if(!empty($brand_goods)){
		$insert_val = '';
		foreach($brand_goods as $val){
			
			if($insert_val != ''){
				$insert_val.= ',';
			}
			$insert_val.= "('$val','$new_term_nid', 'shop' , 0)";
		}
		
		
		$sql = "INSERT INTO ".$table_Prefix."relation (detail_nid , term_nid , belong , vip_term) VALUES $insert_val";
		
		sql_log($sql);
		$query = mysql_query($sql) or die ('此次数据库操作失败2');
	}
}
else{
	$sql = "delete FROM  ".$table_Prefix."relation where belong = 'shop' and term_nid = '$new_term_nid'  "; //删除本店不符合的商品
		
		mysql_query($sql);
}
mysql_close($con);



?>






<script type="text/javascript">
    alert("输入信息已保存，点击确认返回继续操作.");
    document.location.href = "term-list.php?nowpageid=<? echo $nowpageid ?>&sortby=<? echo $sortby?>&sys_part=<?
    echo $system_part ?>";
</script>

</body>